package canglang.monitor.cases.action;

import canglang.monitor.cases.model.CaseThreeModel;
import canglang.monitor.cases.service.CaseThreeMgr;
import canglang.monitor.cases.to.SearchThreeTO;
import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.io.OutputStream;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class CaseThreeAction extends BaseAction {
    
    private static final long serialVersionUID = -6528703365322419354L;

     private CaseThreeModel caseThreeModel;

    private CaseThreeMgr caseThreeMgr;

    private PaginatedListHelper helper;

    private SearchThreeTO searchTO;

    public CaseThreeModel getCaseThreeModel() {
        return caseThreeModel;
    }

    public void setCaseThreeModel(CaseThreeModel caseThreeModel) {
        this.caseThreeModel = caseThreeModel;
    }

    public CaseThreeMgr getCaseThreeMgr() {
        return caseThreeMgr;
    }

    public void setCaseThreeMgr(CaseThreeMgr caseThreeMgr) {
        this.caseThreeMgr = caseThreeMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchThreeTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchThreeTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = CaseThreeModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

     public String search() {
        if (searchTO == null) {
            searchTO = new SearchThreeTO();
        }
        UserModel user=(UserModel)getRequest().getSession().getAttribute("user");
        if(!user.getUserRole().equalsIgnoreCase("A")){
            searchTO.setCreateId(String.valueOf(user.getDeptId()));
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("CREATE_DATE");
        }
        helper = caseThreeMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String delete() {
        for (int i = 0; i < helper.getList().size(); i++) {
            CaseThreeModel u = (CaseThreeModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                caseThreeMgr.deleteCaseThree(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return search();
    }

    public String add() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseThreeModel.setCreateId(String.valueOf(user.getDeptId()));
        caseThreeModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseThreeModel.setCreateDt(date);
        caseThreeModel.setUpdateDt(date);
        caseThreeModel.setUploadCnt(FileUtil.convertToByte(caseThreeModel.getUpload()));
        caseThreeModel.setUploadFileName(caseThreeModel.getUploadFileName());
        caseThreeModel.setUpload1Cnt(FileUtil.convertToByte(caseThreeModel.getUpload1()));
        caseThreeModel.setUpload2Cnt(FileUtil.convertToByte(caseThreeModel.getUpload2()));
        caseThreeModel=caseThreeMgr.addCaseThree(caseThreeModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String view() {
        String selectId = caseThreeModel.getSelectId();
        caseThreeModel= caseThreeMgr.getCaseThree(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String save() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseThreeModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseThreeModel.setUpdateDt(date);
        if(StringUtil.isNullOrBlank(caseThreeModel.getUploadFileName())){
            CaseThreeModel c=caseThreeMgr.getCaseThree(caseThreeModel.getCaseThreeId());
            caseThreeModel.setUploadFileName(c.getUploadFileName());
            caseThreeModel.setUploadCnt(c.getUploadCnt());
        }else{
            caseThreeModel.setUploadCnt(FileUtil.convertToByte(caseThreeModel.getUpload())); 
        }
        if (StringUtil.isNullOrBlank(caseThreeModel.getUpload1FileName())) {
            CaseThreeModel c = caseThreeMgr.getCaseThree(caseThreeModel.getCaseThreeId());
            caseThreeModel.setUpload1FileName(c.getUpload1FileName());
            caseThreeModel.setUpload1Cnt(c.getUpload1Cnt());
        } else {
            caseThreeModel.setUpload1Cnt(FileUtil.convertToByte(caseThreeModel.getUpload1()));
        }
        if (StringUtil.isNullOrBlank(caseThreeModel.getUpload2FileName())) {
            CaseThreeModel c = caseThreeMgr.getCaseThree(caseThreeModel.getCaseThreeId());
            caseThreeModel.setUpload2FileName(c.getUpload2FileName());
            caseThreeModel.setUpload2Cnt(c.getUpload2Cnt());
        } else {
            caseThreeModel.setUpload2Cnt(FileUtil.convertToByte(caseThreeModel.getUpload2()));
        }
        caseThreeModel=caseThreeMgr.saveCaseThree(caseThreeModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

    public String download() throws Exception {
        try {
            String selectId = caseThreeModel.getSelectId();
            String recordType = caseThreeModel.getSelectType();
            caseThreeModel = caseThreeMgr.getCaseThree(Integer.parseInt(selectId));
            getResponse().reset();
            if ("1".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseThreeModel.getUpload1FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseThreeModel.getUpload1Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("2".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseThreeModel.getUpload2FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseThreeModel.getUpload2Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("0".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseThreeModel.getUploadFileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseThreeModel.getUploadCnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }


}
